/**
 * 测试other_nutrients功能
 * 验证营养计算工具函数是否正确处理other_nutrients
 */

// 模拟Food和FoodIntake类型
const testFood1 = {
  id: 1,
  name_zh: '测试食物1',
  name_en: 'Test Food 1',
  energy_kcal: 100,
  protein_g: 10,
  fat_g: 5,
  carb_g: 15,
  fiber_g: 2,
  sodium_mg: 100,
  calcium_mg: 50,
  iron_mg: 2,
  vitamin_c_mg: 10,
  other_nutrients: [
    {
      nutrient_id: 1,
      name_zh: 'Omega-3',
      amount: 500,
      unit: 'mg',
      rich: true
    },
    {
      nutrient_id: 2,
      name_zh: 'DHA',
      amount: 200,
      unit: 'mg',
      rich: true
    }
  ]
}

const testFood2 = {
  id: 2,
  name_zh: '测试食物2',
  name_en: 'Test Food 2',
  energy_kcal: 150,
  protein_g: 8,
  fat_g: 7,
  carb_g: 20,
  fiber_g: 3,
  sodium_mg: 150,
  calcium_mg: 80,
  iron_mg: 3,
  vitamin_c_mg: 15,
  other_nutrients: [
    {
      nutrient_id: 1,
      name_zh: 'Omega-3',
      amount: 300,
      unit: 'mg',
      rich: false
    },
    {
      nutrient_id: 3,
      name_zh: '维生素D',
      amount: 10,
      unit: 'μg',
      rich: true
    }
  ]
}

// 模拟calculateOtherNutrients函数
function calculateOtherNutrients(food, amountG) {
  if (!food.other_nutrients || food.other_nutrients.length === 0) {
    return []
  }

  const ratio = amountG / 100

  return food.other_nutrients.map(nutrient => ({
    ...nutrient,
    amount: nutrient.amount ? Math.round(nutrient.amount * ratio * 10) / 10 : undefined
  }))
}

// 模拟mergeOtherNutrients函数
function mergeOtherNutrients(foodIntakes) {
  const nutrientMap = new Map()

  foodIntakes.forEach(intake => {
    const otherNutrients = calculateOtherNutrients(intake.food, intake.amount_g)
    
    otherNutrients.forEach(nutrient => {
      const key = nutrient.nutrient_id
      const existing = nutrientMap.get(key)
      
      if (existing) {
        existing.amount = (existing.amount || 0) + (nutrient.amount || 0)
        existing.amount = Math.round(existing.amount * 10) / 10
        existing.rich = existing.rich || nutrient.rich
      } else {
        nutrientMap.set(key, { ...nutrient })
      }
    })
  })

  return Array.from(nutrientMap.values())
}

// 测试用例
console.log('=== 测试other_nutrients功能 ===\n')

// 测试1: 计算单个食物的other_nutrients
console.log('测试1: 计算100g食物1的other_nutrients')
const result1 = calculateOtherNutrients(testFood1, 100)
console.log('结果:', JSON.stringify(result1, null, 2))
console.log('✓ 应该返回原始值\n')

// 测试2: 计算50g食物的other_nutrients
console.log('测试2: 计算50g食物1的other_nutrients')
const result2 = calculateOtherNutrients(testFood1, 50)
console.log('结果:', JSON.stringify(result2, null, 2))
console.log('✓ Omega-3应该是250mg, DHA应该是100mg\n')

// 测试3: 合并多个食物的other_nutrients
console.log('测试3: 合并100g食物1和100g食物2的other_nutrients')
const foodIntakes = [
  { food: testFood1, amount_g: 100 },
  { food: testFood2, amount_g: 100 }
]
const result3 = mergeOtherNutrients(foodIntakes)
console.log('结果:', JSON.stringify(result3, null, 2))
console.log('✓ Omega-3应该是800mg (500+300), rich应该是true')
console.log('✓ DHA应该是200mg')
console.log('✓ 维生素D应该是10μg\n')

// 测试4: 合并不同量的食物
console.log('测试4: 合并50g食物1和200g食物2的other_nutrients')
const foodIntakes2 = [
  { food: testFood1, amount_g: 50 },
  { food: testFood2, amount_g: 200 }
]
const result4 = mergeOtherNutrients(foodIntakes2)
console.log('结果:', JSON.stringify(result4, null, 2))
console.log('✓ Omega-3应该是850mg (250+600)')
console.log('✓ DHA应该是100mg')
console.log('✓ 维生素D应该是20μg\n')

console.log('=== 所有测试完成 ===')

